'##################################################
'###  Top Level (Get root Path from system env)
'##################################################
function GetEnv(envVarName)
	On Error Resume Next
	' 创建Shell对象并获取值
	Set WshShell = CreateObject("WScript.Shell")
	envValue = WshShell.Environment("PROCESS")(envVarName) ' 优先查找进程级变量

	' 如果未找到则尝试系统级变量
	If Err.Number <> 0 Or envValue = "" Then
		Err.Clear
		envValue = WshShell.Environment("SYSTEM")(envVarName)
	End If

	' 如果仍未找到则尝试用户级变量
	If Err.Number <> 0 Or envValue = "" Then
		Err.Clear
		envValue = WshShell.Environment("USER")(envVarName)
	End If

	' 检查结果并输出
	If Err.Number = 0 And envValue <> "" Then
		GetEnv=envValue
	Else
		GetEnv=""
	End If
end function

'##################################################
'##################################################
'##################################################
sub IncludeVbs(filepath)
	Dim fso, file, content
	Set fso = CreateObject("Scripting.FileSystemObject")
	' 读取functions.vbs的内容
	Set file = fso.OpenTextFile(filepath, 1) ' 1表示只读
	content = file.ReadAll
	file.Close
	' 将functions.vbs的内容载入全局作用域
	ExecuteGlobal content
end sub

'##################################################
'##################################################
'##################################################
dim root,tmpfile
Dim doc
Set doc = application.ActiveDocument
main
set doc = nothing
sub main()	
	'// REUSE
	envVarName ="EXP_SHORTCUTS_ROOT"
	root = GetEnv(envVarName)'"C:\MentorGraphics\2005EXP\SDD_HOME\pvtk\env\"
	if root ="" then 
		msgbox "please set env value (EXP_SHORTCUTS_ROOT = ?)"
		exit sub
	end if
	
	call IncludeVbs(root & "\skill_lib.vbs")
	
	If ValidateServer(doc) = 0 Then
	    msgbox "Server validation failed."
	    exit sub
	End If	
	
	tmpfile = doc.path & "output\tmp.vbs"
	'// TODO
	dim FileRootPath:FileRootPath = root & "\cache\XY\"
	
	Dim fso, f1
	Dim EditBoxFileName
	Dim outputFileName, FilePathName, outputString
	Dim pcbComps, pcbComp, pcbCompRef, pcbCompX, pcbCompY, pcbCompSide, CompSide, pcbCompRot
	Dim Msg, i, pcbUnits, Units
	Set fso = CreateObject("Scripting.FileSystemObject")
	EditBoxFileName = Inputbox("you enter cg"& vbcrlf &"please input the filename for GetXY position",EditBoxFileName)
	outputFileName = EditBoxFileName & ".txt"
	If outputFileName = "" Then
		Msg = "The file name was not specified."
		application.Gui.Display Msg
	End If
	' Create output file in job directory
	FilePathName = FileRootPath & outputFileName
	'msgbox FilePathName
	Set f1 = fso.CreateTextFile(FilePathName, True)
	'msgbox "here"

	' Write Units
	pcbUnits = doc.CurrentUnit
	If pcbUnits = 2 Then
		Units = "th"
		End If
	If pcbUnits = 3 Then
		Units = "inch"
		End If	
	If pcbUnits = 4 Then
		Units = "mm"
		End If	
	If pcbUnits = 5 Then
		Units = "um"
		End If	
	outputString = ".units " & Units
	f1.WriteLine(outputString)

	' Output the Selected Comps
	Set pcbComps = doc.Components(epcbSelectSelected)
	
	
	If pcbComps.Count = 0 Then
		Msg = "No Selected Parts." & vbCrlf & "Select Parts to Export."
		application.Gui.Display Msg
		Exit Sub
		End If
	
	
	pcbComps.Sort
	For Each pcbComp In pcbComps
		pcbCompRef = pcbComp.Name
		'msgbox pcbCompRef
		pcbCompX = pcbComp.PositionX
		pcbCompY = pcbComp.PositionY
		pcbCompRot = pcbComp.Orientation
		pcbCompSide = pcbComp.Side
		
		If pcbCompSide = 512 Then
			CompSide = "bottom"
			End If
		If pcbCompSide = 1 Then
			CompSide = "top"
			End If		
		'If (pcbComp.Selected = True) Then
			'msgbox pcbCompRef
			outputString = ".ref " & pcbCompRef & " " & pcbCompX & "," & pcbCompY & " " & pcbCompRot & " " & CompSide
			f1.WriteLine(outputString)
		'End If
	Next
	Call application.Gui.CursorBusy(False)
	application.UnlockServer

	f1.Close()
	set f1 = nothing
	Set fso = Nothing
end sub

